package com.agileboot.attendance.domain.supplier.model;

import cn.hutool.core.bean.BeanUtil;
import com.agileboot.attendance.domain.personnel.db.AttPersonnelService;
import com.agileboot.attendance.domain.supplier.command.AddSupplierCommand;
import com.agileboot.attendance.domain.supplier.command.UpdateSupplierCommand;
import com.agileboot.attendance.domain.supplier.db.AttSupplierEntity;
import com.agileboot.attendance.domain.supplier.db.AttSupplierService;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * <p>
 * 供应商领域模型
 * </p>
 *
 * @author agileboot
 * @since 2023-03-14
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class SupplierModel extends AttSupplierEntity {

    private AttSupplierService supplierService;
    private AttPersonnelService personnelService;

    /**
     * 创建领域模型
     * 
     * @param supplierService 供应商服务
     * @param personnelService 人员服务
     */
    public SupplierModel(AttSupplierService supplierService, AttPersonnelService personnelService) {
        this.supplierService = supplierService;
        this.personnelService = personnelService;
    }

    /**
     * 从实体创建领域模型
     * 
     * @param entity 实体
     * @param supplierService 供应商服务
     * @param personnelService 人员服务
     */
    public SupplierModel(AttSupplierEntity entity, AttSupplierService supplierService, AttPersonnelService personnelService) {
        this(supplierService, personnelService);
        if (entity != null) {
            BeanUtil.copyProperties(entity, this);
        }
    }

    /**
     * 加载添加命令数据
     * 
     * @param command 添加命令
     */
    public void loadAddCommand(AddSupplierCommand command) {
        if (command != null) {
            BeanUtil.copyProperties(command, this, "supplierId");
        }
    }

    /**
     * 加载更新命令数据
     * 
     * @param command 更新命令
     */
    public void loadUpdateCommand(UpdateSupplierCommand command) {
        if (command != null) {
            loadAddCommand(command);
            this.setSupplierId(command.getSupplierId());
        }
    }

    /**
     * 检查供应商编码是否唯一
     */
    public void checkSupplierCodeIsUnique() {
        if (supplierService.isSupplierCodeDuplicated(getSupplierCode(), getSupplierId())) {
            throw new ApiException(ErrorCode.Business.SUPPLIER_CODE_IS_NOT_UNIQUE);
        }
    }

    /**
     * 检查供应商名称是否唯一
     */
    public void checkSupplierNameIsUnique() {
        if (supplierService.isSupplierNameDuplicated(getSupplierName(), getSupplierId())) {
            throw new ApiException(ErrorCode.Business.SUPPLIER_NAME_IS_NOT_UNIQUE);
        }
    }

    /**
     * 检查供应商是否可以删除
     */
    public void checkCanBeDelete() {
        long personnelCount = personnelService.countBySupplierId(getSupplierId());
        if (personnelCount > 0) {
            throw new ApiException(ErrorCode.Business.SUPPLIER_HAS_PERSONNEL);
        }
    }
} 